//
//  RJNavigationBar.m
//  导航栏滚动显示与隐藏
//
//  Created by XinHuiOS on 2019/8/13.
//  Copyright © 2019 XinHuiOS. All rights reserved.
//

#import "RJNavigationBar.h"
#import "Masonry.h"

//#define HEXCOLOR(c) [UIColor colorWithRed:((c>>16)&0xFF)/255.0 green:((c>>8)&0xFF)/255.0 blue:(c&0xFF)/255.0 alpha:1.0]
//#define colorBlue   HEXCOLOR(0x00C3F5)
//#define colorWhite  HEXCOLOR(0xFFFFFF)
//
//#define WySCREENWIDTH      [[UIScreen mainScreen] bounds].size.width
//#define kHeight     [[UIScreen mainScreen] bounds].size.height
//#define INTERFACE_IS_IPHONEX (@available(iOS 11.0, *) && ([UIApplication sharedApplication].keyWindow.safeAreaInsets.bottom > 0)?YES:NO)
////为刘海屏 新增两个宏定义导航栏和工具栏的高度
//#define nav_Height (INTERFACE_IS_IPHONEX ? 88 : 64)
//#define tabBar_Height (INTERFACE_IS_IPHONEX ? 83 : 49)
//// 判断是否是iPhone X
//#define iPhoneX ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(1125, 2436), [[UIScreen mainScreen] currentMode].size) : NO)
////判断iPhoneX，Xs（iPhoneX，iPhoneXs）
//#define IS_IPHONE_X ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(1125, 2436), [[UIScreen mainScreen] currentMode].size) : NO)
////判断iPhoneXr
//#define IS_IPHONE_Xr ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(828, 1792), [[UIScreen mainScreen] currentMode].size)  : NO)
////判断iPhoneXsMax
//#define IS_IPHONE_Xs_Max ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(1242, 2688), [[UIScreen mainScreen] currentMode].size) : NO)
//#define IS_PhoneXAll (IS_IPHONE_X || IS_IPHONE_Xr || IS_IPHONE_Xs_Max)
///** 顶部安全距离 */
//#define kSafeAreaTop ({\
//CGFloat statusBarHeight = 0.;\
//if(IS_PhoneXAll)statusBarHeight = 24.;\
//statusBarHeight;\
//})
//#define kSafeAreaTop ({\
//CGFloat statusBarHeight = 0.;\
//if(IS_PhoneXAll)statusBarHeight = 24.;\
//statusBarHeight;\
//})
@interface RJNavigationBar ()
@property (nonatomic, strong)UILabel * titleLabel;
@property (nonatomic, strong)UIButton * leftButton;
@property (nonatomic, strong)UIButton * rightButton;
@property (nonatomic, strong)UIImageView *bgBroundView;

@end


@implementation RJNavigationBar

+(instancetype)createCustomNavigationBar {
    
    RJNavigationBar * navigationBar =[[self alloc] initWithFrame:CGRectMake(0,0,WySCREENWIDTH,nav_Height)];
    return navigationBar;
}

#pragma mark - life cycle
-(instancetype)init {
    if (self =[super init]) {
        [self setup];
    }
    return self;
}
-(instancetype)initWithFrame:(CGRect)frame {
    if (self =[super initWithFrame:frame]) {
        [self setup];
    }
    return self;
    
}
-(void)setup {
   //配置默认设置
    [self setDefault];
    [self setupSubViews];
    [self setupSuViewsConstraonts];
}
-(void)setDefault {
    
    self.titleLabel.textColor = colorWhite;
    self.backgroundColor = self.navBackgroundColor = colorBlue;
    self.bgBroundView.alpha = 0.0;
    self.backgroundColor = [self.navBackgroundColor colorWithAlphaComponent:0.0];
    
}
#pragma mark - add subViews
-(void)setupSubViews {
    
    [self addSubview:self.bgBroundView];
    [self addSubview:self.leftButton];
    [self addSubview:self.titleLabel];
    [self addSubview:self.rightButton];
    
}

#pragma mark - layout subviews
-(void)setupSuViewsConstraonts {
    
    [self.bgBroundView mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.edges.equalTo(self);
        make.size.equalTo(CGSizeMake(WySCREENWIDTH, CNavigationStatusHeight));
        make.centerX.mas_offset(0);
        make.top.mas_offset(0);
    }];
    [self.leftButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self);
        make.size.mas_equalTo(CGSizeMake(44,44));
        make.top.equalTo(self).offset(15+kSafeAreaTop);
    }];
    
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(180,44));
        make.top.mas_equalTo(15+kSafeAreaTop);
        make.left.mas_equalTo((WySCREENWIDTH-180)/2);
        
    }];
    
    [self.rightButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-20);
        make.height.mas_equalTo(44);
        make.top.mas_equalTo(15+kSafeAreaTop);
        
    }];
    
}
#pragma mark - public methods
-(void)setupBgColorAlpha:(CGFloat)alpha animation:(NSTimeInterval)duration compeleteBlock:(void (^)(void))compeleteBlock {
    
    [UIView animateWithDuration:duration animations:^{
        self.bgBroundView.alpha = alpha;
        self.titleLabel.textColor = [[UIColor blackColor] colorWithAlphaComponent:alpha];
        if (compeleteBlock) {
            compeleteBlock();
        }
    }];
}

-(void)setupBgImageAlpha:(CGFloat)alpha animation:(NSTimeInterval)duration compeleteBlock:(void (^)(void))compeleteBlock {
    
    [UIView animateWithDuration:duration animations:^{
        self.bgBroundView.alpha = alpha;
        self.titleLabel.textColor = [colorWhite colorWithAlphaComponent:alpha];
        if (compeleteBlock) {
            compeleteBlock();
        }
    }];
    
}

-(void)setLeftItemImage:(NSString *)imgName leftItemTitle:(NSString *)leftItemtitle textColor:(UIColor *)color {
    [self.leftButton setImage:[UIImage imageNamed:imgName] forState:UIControlStateNormal];
    [self.leftButton setTitle:leftItemtitle forState:UIControlStateNormal];
    [self.leftButton setTitleColor:color forState:UIControlStateNormal];
}

-(void)setRightItemImage:(NSString *)imgName RightItemTitle:(NSString *)rightItemtitle textColor:(UIColor *)color {
    [self.rightButton setImage:[UIImage imageNamed:imgName] forState:UIControlStateNormal];
    [self.rightButton setTitle:rightItemtitle forState:UIControlStateNormal];
    [self.rightButton setTitleColor:color forState:UIControlStateNormal];
}

-(void)setLeftItemImage:(NSString *)imgName {
    [self.leftButton setImage:[UIImage imageNamed:imgName] forState:UIControlStateNormal];
}
-(void)setRightItemImage:(NSString *)imgName {
    [self.rightButton setImage:[UIImage imageNamed:imgName] forState:UIControlStateNormal];
}

-(void)showCustomNavigationBar:(BOOL)show {
    self.hidden =!show;
    
}

#pragma mark - private methods
-(void)setTitle:(NSString *)title {
    _title = title;
    self.titleLabel.text = title;
}
-(void)setTitleColor:(UIColor *)titleColor {
    _titleColor = titleColor;
    self.titleLabel.textColor = titleColor;
    
}
-(void)setNavBackgroundColor:(UIColor *)navBackgroundColor {
    
    _navBackgroundColor = navBackgroundColor;
    self.backgroundColor = navBackgroundColor;
    
}
-(void)setNavBackgroundImage:(UIImage *)navBackgroundImage {
    _navBackgroundImage = navBackgroundImage;
    self.bgBroundView.image = navBackgroundImage;
}

#pragma mark - event response
-(void)clickBack {
    if (self.clickleftItemBlock) {
        self.clickleftItemBlock();
    }
}
-(void)clickRight {
    if (self.clickrightItemBlock) {
        self.clickrightItemBlock();
    }
}

#pragma mark lazyLoad
-(UIButton *)leftButton {
    if (!_leftButton) {
        _leftButton =[[UIButton alloc] init];
        [_leftButton setImage:[UIImage imageNamed:@"flow备份"] forState:UIControlStateNormal];
        [_leftButton addTarget:self action:@selector(clickBack) forControlEvents:UIControlEventTouchUpInside];
    }
    return _leftButton;
}

-(UIButton *)rightButton {
    if (!_rightButton) {
        _rightButton =[[UIButton alloc] init];
        [_rightButton setImage:[UIImage imageNamed:@"icon_back"] forState:UIControlStateNormal];
        [_rightButton addTarget:self action:@selector(clickRight) forControlEvents:UIControlEventTouchUpInside];
    }
    return _rightButton;
}

-(UILabel *)titleLabel {
    if (!_titleLabel) {
        _titleLabel =[[UILabel alloc] init];
        _titleLabel.font =[UIFont systemFontOfSize:18];
        _titleLabel.textAlignment = NSTextAlignmentCenter;
    }
    return _titleLabel;
}

-(UIImageView *)bgBroundView {
    if (!_bgBroundView) {
        _bgBroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@""]];
        _bgBroundView.backgroundColor = WyWhiteColor;
    }
    return _bgBroundView;
}
@end
